package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/alert-rule")
@Api(tags = "报警规则")
public class AlertRuleController extends BaseController {
    @Autowired
    private AlertRuleService alertRuleService;

    @PostMapping("/create")
    @ApiOperation("新增报警规则")
    private ResponseResult add(@RequestBody AlertRuleDto dto) {
        alertRuleService.add(dto);
        return success();
    }

    @GetMapping("/get-page")
    @ApiOperation("分页获取告警规则列表")
    private ResponseResult<PageResponse<AlertRuleVo>> findPageList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                   String alertRuleName,
                                                                   String functionName,
                                                                   String productKey) {
        PageResponse<AlertRuleVo> response = alertRuleService.findPageList(pageNum, pageSize, alertRuleName, functionName, productKey);
        return success(response);
    }

    @GetMapping("/read/{id}")
    @ApiOperation("获取单个告警规则")
    private ResponseResult<AlertRuleVo> findById(@PathVariable Integer id) {
        return success(alertRuleService.findById(id));
    }

    @PutMapping("/update/{id}")
    @ApiOperation("更新告警规则")
    private ResponseResult updateRule(@RequestBody AlertRuleDto dto) {
        alertRuleService.updateRule(dto);
        return success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除告警规则")
    private ResponseResult<AlertRuleVo> deleteById(@PathVariable Integer id) {
        alertRuleService.deleteById(id);
        return success();
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation("启用-禁用")
    private ResponseResult updateStatus(@PathVariable Integer id,
                                        @PathVariable Integer status) {
        alertRuleService.updateStatus(id, status);
        return success();
    }
}
